package com.lfy.fyadmin.util;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import java.lang.reflect.Field;
import java.util.Objects;

public class UpdateUtil {

    /**
     * 将驼峰命名转换为下划线命名
     * @param camelCase 驼峰命名字符串
     * @return 下划线命名字符串
     */
    private static String camelToUnderscore(String camelCase) {
        if (camelCase == null || camelCase.isEmpty()) {
            return camelCase;
        }
        
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < camelCase.length(); i++) {
            char c = camelCase.charAt(i);
            if (Character.isUpperCase(c)) {
                if (i > 0) {
                    result.append('_');
                }
                result.append(Character.toLowerCase(c));
            } else {
                result.append(c);
            }
        }
        return result.toString();
    }

    /**
     * 按需更新，只更新非null且非空字符串的字段
     * @param entity 实体对象
     * @param id 主键ID
     * @param <T> 实体类型
     * @return UpdateWrapper
     */
    public static <T> UpdateWrapper<T> updateWrapper(T entity, Long id) {
        UpdateWrapper<T> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);

        Class<?> clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(entity);
                // 跳过id字段，避免重复设置
                if ("id".equals(field.getName())) {
                    continue;
                }
                // 只更新非null且非空字符串的字段
                if (value != null && !(value instanceof String && ((String) value).isEmpty())) {
                    // 将驼峰命名转换为下划线命名，符合MyBatis-Plus的驼峰转换规则
                    String columnName = camelToUnderscore(field.getName());
                    updateWrapper.set(columnName, value);
                }
            } catch (IllegalAccessException e) {
                // 忽略无法访问的字段
            }
        }

        return updateWrapper;
    }

    /**
     * 按需更新，从实体对象中自动获取ID
     * @param entity 实体对象
     * @param <T> 实体类型
     * @return UpdateWrapper
     */
    public static <T> UpdateWrapper<T> updateWrapper(T entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity cannot be null");
        }

        Long id = null;
        Class<?> clazz = entity.getClass();
        
        // 获取ID字段的值
        try {
            Field idField = clazz.getDeclaredField("id");
            idField.setAccessible(true);
            id = (Long) idField.get(entity);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException("Cannot access id field from entity: " + clazz.getSimpleName(), e);
        }

        if (id == null) {
            throw new IllegalArgumentException("Entity id cannot be null for update operation");
        }

        return updateWrapper(entity, id);
    }
}